home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / glibc108.gz / glibc108 / glibc-1.08.1 / manual / examples / rprintf.c < prev    next >
C/C++ Source or Header  |  1994-02-16  |  1KB  |  53 lines

  1. #include <stdio.h>
  2. #include <printf.h>
  3. #include <stdarg.h>
  4.  
  5. /*@group*/
  6. typedef struct
  7.   {
  8.     char *name;
  9.   } Widget;
  10. /*@end group*/
  11.  
  12. int 
  13. print_widget (FILE *stream, const struct printf_info *info, va_list *app)
  14. {
  15.   Widget *w;
  16.   char *buffer;
  17.   int len;
  18.  
  19.   /* Format the output into a string. */
  20.   w = va_arg (*app, Widget *);
  21.   len = asprintf (&buffer, "<Widget %p: %s>", w, w->name);
  22.   if (len == -1)
  23.     return -1;
  24.  
  25.   /* Pad to the minimum field width and print to the stream. */
  26.   len = fprintf (stream, "%*s",
  27.          (info->left ? - info->width : info->width),
  28.          buffer);
  29.  
  30.   /* Clean up and return. */
  31.   free (buffer);
  32.   return len;
  33. }
  34.  
  35.  
  36. int
  37. main (void)
  38. {
  39.   /* Make a widget to print. */
  40.   Widget mywidget;
  41.   mywidget.name = "mywidget";
  42.  
  43.   /* Register the print function for widgets. */
  44.   register_printf_function ('W', print_widget, NULL); /* No arginfo.  */
  45.  
  46.   /* Now print the widget. */
  47.   printf ("|%W|\n", &mywidget);
  48.   printf ("|%35W|\n", &mywidget);
  49.   printf ("|%-35W|\n", &mywidget);
  50.  
  51.   return 0;
  52. }
  53.